package com.springinpractice.ch07.service;

import java.io.IOException;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.inject.Inject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.springinpractice.ch07.model.Article;
import com.springinpractice.core.dao.Dao;
import com.springinpractice.ch07.model.Article;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = true)
public class ArticleServiceImpl implements ArticleService
{

	@Inject private Dao<Article> articleDao;

	//other service methods (just call corresponding DAO methods)
	
	@Override
	public void deleteAllArticles()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void deleteArticle(String id)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void deleteArticle(Article article)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void exportArticle(String id, OutputStream out) throws IOException
	{
		// TODO Auto-generated method stub

	}

	@Override
	public Article getArticle(String id)
	{
		// TODO Auto-generated method stub
		return articleDao.get(id);
	}

	@Override
	public long getArticleCount()
	{
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Article> getArticles()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void importArticle(InputStream in) throws IOException
	{
		// TODO Auto-generated method stub

	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	@Override
	public void saveArticle(Article article)
	{
		String articleId = article.getId();
		if (article.getDateCreated() == null)
		{
			if (!articleDao.exists(articleId))
			{
				articleDao.create(article);
			}
			else
			{
				throw new RuntimeException("Article ID already exists");
			}
		}
		else
		{
			articleDao.update(article);
		}
	}
	
	

}
